package com.xz.group12306.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xz.group12306.business.service.Impl.UserServiceImpl;
import com.xz.group12306.domain.User;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// String target = "";
		// 修改乱码
		/*request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");*/
		// 获取页面的值
		String username = request.getParameter("username");
		//String password = MD5Servlet.md5(request.getParameter("password"));
		String password = request.getParameter("password");

		String rule = request.getParameter("rule");
		// 输入框中的验证码
		String vvcode = request.getParameter("vvcode");
		// 图片中的验证码值
		String vcode = (String) request.getSession().getAttribute("code");

		UserServiceImpl userServiceImpl = new UserServiceImpl();
		User user=userServiceImpl.login(username, MD5Servlet.md5(password), rule);

		// 判断用户名密码验证码是否为空
		if (!(username == null) && !(password == null) && !(vvcode == null)) {
			// 判断验证码是否一致
			if (vcode.equals(vvcode)) {
				if (user != null) {
					// 查询用户不为空并将值存入session中
					request.getSession().setAttribute("user", user);
					// 判断角色跳转
					if ("1".equals(rule)) {
						// target = "user/login.html";
						response.getWriter().print("<script>window.location.href='user/admin.html';</script>");
					} else if ("2".equals(rule)) {
						// target = "user/index.html";
						response.getWriter().print("<script>window.location.href='user/index.jsp';</script>");
						System.out.println("登陆成功");
					}
				}
				if (user == null) {
					response.getWriter().print(
							"<script>alert(\"用户名密码输入错误,请重新登录\");window.location.href='user/login.html';</script>");
					// target = "user/login.html";
				}
			} else {
				// target = "user/login.html";
				response.getWriter()
						.print("<script>alert(\"验证码输入错误,请重新登录\");window.location.href='user/login.html';</script>");
			}
		} else {
			response.getWriter()
					.print("<script>alert(\"用户名，密码，验证码不能为空\");window.location.href='user/login.html';</script>");
		}
		/*
		 * RequestDispatcher dispatcher=request.getRequestDispatcher(target);
		 * dispatcher.forward(request, response);
		 */

	}

}
